package com.hk.admin.module.system.controller;

import com.hk.admin.common.base.controller.BaseController;
import com.hk.admin.common.base.dto.message.Message;
import com.hk.admin.module.system.service.MenuService;
import com.hk.admin.module.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;

/**
 * <br>
 * <b>功能：</b>用于处理angular路由，防止刷新404<br>
 * <b>作者：</b>Pan.ShiJu<br>
 * <b>日期：</b>2017/4/11 23:27<br>
 */
@Controller
@RequestMapping("/")
public class MainController extends BaseController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    /**
     * 默认处理/main下说有的请求，全部转发到index.html
     */
    @RequestMapping("")
    public void index(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("routes", "路由跳转");
        try {
            request.getRequestDispatcher("index.html").forward(request, response);
        } catch (Exception es) {
        }
    }

    /**
     * 默认处理/main下说有的请求，全部转发到index.html
     */
    @RequestMapping("/main/**")
    public void routes(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("routes", "路由跳转");
        try {
            // 此处路径要打两点，如果直接写 index.html 会循环反问/web/index.html 造成死循环
            request.getRequestDispatcher("../index.html").forward(request, response);
        } catch (Exception es) {
        }
    }


    /**
     * 当前登录人的菜单
     */
    @RequestMapping(value = "/menu/list", method = RequestMethod.GET)
    public ResponseEntity<Message> getMenuList(Principal principal) {
        return menuService.getMenuList(principal.getName());
    }

    /**
     * 当前登录人的角色
     */
    @RequestMapping(value = "/role/list", method = RequestMethod.GET)
    public ResponseEntity<Message> getRoleList(Principal principal) {
        return roleService.getRoleList(principal.getName());
    }


}
